package com.yanftch.json2ets.json2ets.generator;

import com.intellij.ide.projectView.ProjectView;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.vfs.VirtualFile;
import com.yanftch.json2ets.json2ets.exceptions.FileIOException;
import com.yanftch.json2ets.json2ets.parser.typescript.ParseType;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;

public final class GeneratorDelegateJava {
    private final MessageDelegate messageDelegate;

    public GeneratorDelegateJava(MessageDelegate messageDelegate) {
        this.messageDelegate = messageDelegate;
    }

    public final void runGeneration(final AnActionEvent event, final String json, String rootName, final ParseType parseType, final boolean generateSetGet) {
        if (event == null) {
            messageDelegate.showMessage("wrong json ...", event);
            return;
        }
        if (rootName == null || "".equals(rootName)) {
            rootName = "RootObject";
        }
        String finalRootName = rootName;
        ProgressManager.getInstance().run((Task) (new Task.Backgroundable(event.getProject(), "ets code is generating....", false) {
            public void run(@NotNull final ProgressIndicator indicator) {
                try {
                    TsGenerator generator = new TsGenerator();
                    generator.generateFromJsonByDocument(json, event, finalRootName, parseType, generateSetGet);
                    messageDelegate.showMessage("ets interface has been generated", event);
                } catch (Exception e) {
                    if (e instanceof IOException) {
                        messageDelegate.catchException(new FileIOException(), event);
                    } else {
                        messageDelegate.catchException(e, event);
                    }
                } finally {
                    indicator.stop();
                    if (event.getProject() != null) {
                        ProjectView.getInstance(event.getProject()).refresh();
                    }
                    VirtualFile data = event.getData(LangDataKeys.VIRTUAL_FILE);
                    if (data != null) {
                        data.refresh(false, true);
                    }
                }
            }
        }));
    }

    public final void runGenerationToFile(final AnActionEvent event, final String json, String rootName, final ParseType parseType) {
        if (event == null) {
            messageDelegate.showMessage("wrong json ...", event);
            return;
        }
        if (rootName == null || "".equals(rootName)) {
            rootName = "RootObject";
        }
        String finalRootName = rootName;
        ProgressManager.getInstance().run((Task) (new Task.Backgroundable(event.getProject(), "ets code is generating....", false) {
            public void run(@NotNull final ProgressIndicator indicator) {
                try {
                    TsGenerator generator = new TsGenerator();
                    generator.generateTsFile(json, event, finalRootName, parseType);
                    messageDelegate.showMessage("ets interface has been generated", event);
                } catch (Exception e) {
                    if (e instanceof IOException) {
                        messageDelegate.catchException(new FileIOException(), event);
                    } else {
                        messageDelegate.catchException(e, event);
                    }
                } finally {
                    indicator.stop();
                    if (event.getProject() != null) {
                        ProjectView.getInstance(event.getProject()).refresh();
                    }
                    VirtualFile data = event.getData(LangDataKeys.VIRTUAL_FILE);
                    if (data != null) {
                        data.refresh(false, true);
                    }
                }
            }
        }));
    }
}
